#include "CmdManager.h"
#include <iostream>



// 初始化函数，初始化支持的命令及课程信息
void CmdManager::Init()
{
    // 初始化课程列表
    manager.AddCourse("Linux");
    // cout<<manager.courseList[0].GetId()<<endl;
    manager.AddCourse("NodeJS");
    // cout<<manager.courseList[1].GetId()<<endl;
    manager.AddCourse("C++");
    // cout<<manager.courseList[2].GetId()<<endl;
    manager.AddCourse("Python");
    // cout<<manager.courseList[3].GetId()<<endl;
    manager.AddCourse("Spark");
    // cout<<manager.courseList[4].GetId()<<endl;
    manager.AddCourse("Hadoop");
    // cout<<manager.courseList[5].GetId()<<endl;
    manager.AddCourse("Ruby");
    // cout<<manager.courseList[6].GetId()<<endl;
    manager.AddCourse("Java");
    // cout<<manager.courseList[7].GetId()<<endl;

    // 初始化命令列表
    cmdMap.insert(pair<CourseCmd, string>(Cmd_PrintHelp, "Help Info"));
    cmdMap.insert(pair<CourseCmd, string>(Cmd_PrintCourse, "Course List"));
    cmdMap.insert(pair<CourseCmd, string>(Cmd_PrintCourseNum, "Course Number"));
    cmdMap.insert(pair<CourseCmd, string>(Cmd_PrintLongName, "Longest Course Name"));
    cmdMap.insert(pair<CourseCmd, string>(Cmd_RemoveLastCourse, "Remove Last Course"));
    cmdMap.insert(pair<CourseCmd, string>(Cmd_Exit, "Exit"));

}

// 打印帮助信息
void CmdManager::PrintAllHelp() const
{
    cout << "Cmd List:" << endl;
    for(auto x:cmdMap)
    {
        cout<<x.first<<":"<<x.second<<endl;
    }
}

// 根据命令查询帮助信息
void CmdManager::PrintCmdHelp(const CourseCmd cmd) const
{
    auto iter=cmdMap.find(cmd);
    if(iter!=cmdMap.end())
    {
        cout<<iter->first<<":"<<iter->second<<endl;
    }
    else
    {
        cout<<"not found"<<endl;
    }   

}

// 处理命令操作
bool CmdManager::HandleCmd(const CourseCmd cmd)
{

    if(cmdMap.find(cmd)==cmdMap.end())
    {
        cout<<"not found!"<<endl;
        return true;
    }

    switch(cmd)
    {
        case Cmd_PrintHelp:PrintAllHelp();break;
        case Cmd_PrintCourse:manager.PrintAllCourse();break;
        case Cmd_PrintCourseNum:cout<<manager.GetCourseNum()<<endl;break;
        case Cmd_PrintLongName:manager.PrintLongNameCourse();break;
        case Cmd_RemoveLastCourse:manager.RemoveLast();break;
        case Cmd_Exit:return false;
        default:break;
    }

    return true;
}


